package com.hk.devp.captcha.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.code.kaptcha.Constants;
import com.hk.devp.captcha.common.CaptchaConst;
import com.hk.devp.captcha.utils.RPCUtils;

@Controller
public class LoginController {
	
	/**
	* @author: dragon0574@sina.com
	* @date 2017年7月28日 下午7:00:51 
	* @return 
	* @Description:跳转到含验证码的页面
	 */
	@RequestMapping("/")
	public String index(){
		
		return "index";
	}
	/**
	* @author: dragon0574@sina.com
	* @date 2017年7月28日 下午2:47:40 
	* @return 
	* @Description:跳转到滑动图片验证码
	 */
	@RequestMapping("/toSlide")
	public String toSlideCaptcha(){
		
		return "login";
	}
	
	/**
	* @author: dragon0574@sina.com
	* @date 2017年7月27日 下午4:31:35 
	* @param request
	* @return 
	 * @throws Exception 
	* @Description:获取那些不同的验证码
	 */
	@RequestMapping("/login")
	public String login(HttpServletRequest request,RedirectAttributes redirect) throws Exception{
		HttpSession session = request.getSession();
		
		
		// 获取提交的参数
		String calCaptchaName = request.getParameter("calCaptchaName");
		String simpleCaptchaName = request.getParameter("simpleCaptchaName");
		String chineseCaptchaName = request.getParameter("chineseCaptchaName");
		String kCaptchaName = request.getParameter("kCaptchaName");
		
		// 从session中获取验证码的值
		String calCaptcha = session.getAttribute(CaptchaConst.CAL_CAPTCHA_NAME).toString();
		String simpleCaptcha = session.getAttribute(CaptchaConst.SIMPLE_CAPTCHA_NAME).toString();
		String chineseCaptcha = session.getAttribute(CaptchaConst.CHINESE_CAPTCHA_NAME).toString();
		String kCaptcha = session.getAttribute(Constants.KAPTCHA_SESSION_KEY).toString();
		
		System.out.println(calCaptcha.equals(calCaptchaName));
		System.out.println(simpleCaptcha.equals(simpleCaptchaName));
		System.out.println(chineseCaptcha.equals(chineseCaptchaName));
		System.out.println(kCaptcha.equals(kCaptchaName));
		
		
		
		// 对Google的reCaptcha-Service进行处理
		String apiUrl="https://www.google.com/recaptcha/api/siteverify";
		String gcpRspon = request.getParameter("g-recaptcha-response");
		String remoteHost = request.getRemoteHost();
		
		Map<String, String> requestParam=new HashMap<>();
		requestParam.put("response", gcpRspon);
		requestParam.put("remoteip", remoteHost);
		requestParam.put("secret", "6Let1SoUAAAAAF12OEgosteOFycGBzy0EKfC0Q9c");
		
		/*String post = RPCUtils.post(apiUrl, requestParam);
		System.out.println(post);*/
		
		redirect.addAttribute("response", gcpRspon);
		redirect.addAttribute("remoteip", remoteHost);
		redirect.addAttribute("secret", "6Let1SoUAAAAAF12OEgosteOFycGBzy0EKfC0Q9c");
		
		
		/*redirect.addFlashAttribute("response", gcpRspon);
		redirect.addFlashAttribute("remoteip", remoteHost);
		redirect.addFlashAttribute("secret", "6Let1SoUAAAAAF12OEgosteOFycGBzy0EKfC0Q9c");*/
		
		
		
		
		return "redirect:/toReCaptcha";
	}
	
	@RequestMapping("/toReCaptcha")
	public String toReCaptcha(HttpServletRequest request , Model model){
		model.addAttribute("response", request.getParameter("response") );
		model.addAttribute("remoteip", request.getParameter("remoteip"));
		model.addAttribute("secret", request.getParameter("secret"));
		
		return "google-reCaptcha";
	}
	
	
	
	
}
